home *** CD-ROM | disk | FTP | other *** search
/ GameStar 2004 April / Gamestar_61_2004-04_dvdb.iso / DVDStar / Editace / hltp.exe / {app} / Applications / MilkShape 3D / JSSamples / Spikey.js < prev    next >
Text File  |  2003-05-31  |  2KB  |  60 lines

  1. //////////////////////////////////////////////////////////////////////////////
  2. // Pikey.js
  3. // Sample script for the msToolJS plugin. Converts each triangle into a spike.
  4. // By Ulf ╓hlΘn
  5.  
  6. ofs = prompt("Enter spike offset", 10,  "Offset");
  7. ofs = ofs*1;
  8.  
  9. if(ofs!=null)
  10. {
  11.     for(i=0; i<model.meshes.length; i++)
  12.     {
  13.         m = model.meshes[i];
  14.         numTriangles = m.triangles.length;
  15.         for(j=0; j<numTriangles; j++)
  16.         {
  17.             t = m.triangles[j]; // current triangle
  18.  
  19.             // Get the triangle's 3 vertices
  20.             v0 = m.vertices[t.vertexIndices[0]].vertex;
  21.             v1 = m.vertices[t.vertexIndices[1]].vertex;
  22.             v2 = m.vertices[t.vertexIndices[2]].vertex;
  23.  
  24.             // Get triangle normal
  25.             var c1 = new Vec3(v2.x-v0.x, v2.y-v0.y, v2.z-v0.z);
  26.             var c2 = new Vec3(v2.x-v1.x, v2.y-v1.y, v2.z-v1.z);
  27.             n = Vec3.crossProd(c1, c2);
  28.             n.normalize(ofs);
  29.  
  30.             // Get triangle midpoint and translate along the normal
  31.             midp = getMidpoint(v0, v1, v2);
  32.             midp.x+= n.x;
  33.             midp.y+= n.y;
  34.             midp.z+= n.z;
  35.  
  36.             // Add a new vertex at the translated midpoint
  37.             vIdx = m.vertices.length; // Vertex index
  38.             nIdx = m.normals.length; // Normal index            
  39.             m.vertices.push(new Vertex(midp.x, midp.y, midp.z)); 
  40.             m.normals.push(new Vec3(n.x, n.y, n.z));
  41.             
  42.             // Add two new triangles
  43.             m.triangles.push(new Triangle([t.vertexIndices[0], t.vertexIndices[1], vIdx], [t.normalIndices[0], t.normalIndices[1], nIdx]));
  44.             m.triangles.push(new Triangle([t.vertexIndices[2], t.vertexIndices[0], vIdx], [t.normalIndices[2], t.normalIndices[0], nIdx]));
  45.  
  46.             // Redirect the original triangle's first vertex
  47.             t.vertexIndices[0] = vIdx;
  48.         }
  49.     }
  50. }
  51.  
  52. function getMidpoint(p0, p1, p2)
  53. {
  54.     var v = new Vec3();
  55.     v.x = (p0.x + p1.x + p2.x)/3;
  56.     v.y = (p0.y + p1.y + p2.y)/3;
  57.     v.z = (p0.z + p1.z + p2.z)/3;
  58.     return v;
  59. }
  60.